package com.cheyunkeji.er.view.evaluate;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.view.MyRadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/4/11 17:17.
 */

public class EvaluateCheckItemView extends RelativeLayout {

    public static final int PROBLEM_NONE = 0;
    public static final int PROBLEM_LITE = 1;
    public static final int PROBLEM_HEAVY = 2;
    public static final int PROBLEM_NEED_FIX = 3;

//  当前选择项下标值，默认为 -1
    private int checkItemIndexNumber = -1 ;

    @BindView(R.id.tv_check_item_desc)
    TextView tvCheckItemDesc;
    @BindView(R.id.tv_index_number)
    TextView tvIndexNumber;
    @BindView(R.id.ib_take_photo)
    ImageButton ibTakePhoto;
    @BindView(R.id.et_remarks)
    EditText etRemarks;
    @BindView(R.id.rg_multiple_check)
    MyRadioGroup rgMultipleCheck;
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    private View rootView;

    private OnPhotoIconClicked listener ;

    public EvaluateCheckItemView(Context context) {
        super(context);
        initView(context);
    }

    public EvaluateCheckItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EvaluateCheckItemView);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            switch (typedArray.getIndex(i)) {
                case R.styleable.EvaluateCheckItemView_evaluate_check_bkg_color:
                    rlContent.setBackgroundColor(typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
                    break;
                case R.styleable.EvaluateCheckItemView_evaluate_check_desc:
                    tvCheckItemDesc.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.EvaluateCheckItemView_evaluate_check_index_number:
                    checkItemIndexNumber = Integer.valueOf(typedArray.getString(typedArray.getIndex(i))) ;
                    tvIndexNumber.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;

                default:
                    break;
            }
        }

        typedArray.recycle();

    }

    public EvaluateCheckItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initView(Context context) {

        rootView = LayoutInflater.from(context).inflate(R.layout.er_evaluate_check_view_layout, this, true);
        ButterKnife.bind(this, rootView);

        ibTakePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.takePhoto(checkItemIndexNumber);
            }
        });
    }

//    返回CheckBox选择结果
    public int getCheckedResult() {

        return (rgMultipleCheck.getCheckedRadioButtonId() == R.id.rb_none) ? PROBLEM_NONE :
                (rgMultipleCheck.getCheckedRadioButtonId() == R.id.rb_heavy) ? PROBLEM_HEAVY :
                        (rgMultipleCheck.getCheckedRadioButtonId() == R.id.rb_lite) ? PROBLEM_LITE :
                                (rgMultipleCheck.getCheckedRadioButtonId() == R.id.rb_need_fix) ? PROBLEM_NEED_FIX : -1;
    }

    /**
     * 设置RadioGroup当前被选中的选项
     * @param currentState
     */
    public void setCurrentCheckedState(int currentState){

        if(PROBLEM_NONE == currentState) rgMultipleCheck.setCheckWithoutNotif(R.id.rb_none);
        if(PROBLEM_LITE == currentState) rgMultipleCheck.setCheckWithoutNotif(R.id.rb_lite);
        if(PROBLEM_HEAVY == currentState) rgMultipleCheck.setCheckWithoutNotif(R.id.rb_heavy);
        if(PROBLEM_NEED_FIX == currentState) rgMultipleCheck.setCheckWithoutNotif(R.id.rb_need_fix);

    }

    public void setOnPhotoIconClickedListener(OnPhotoIconClicked listener){
        this.listener = listener ;
    }

    public interface OnPhotoIconClicked {

        void takePhoto(int checkItemIndex);
    }

//    返回标注文本
    public String getRemarksText(){
        return  etRemarks.getText().toString().trim();
    }

}
